package com.now.printer.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;

import com.gamerole.orcameralib.CameraActivity;
import com.now.printer.R;
import com.now.printer.base.BaseActivity2;
import com.now.printer.base.BaseFragment2;
import com.now.printer.ui.activity.FileBrowerManagerActivity;
import com.now.printer.ui.activity.FilePreviewAndPrintActivity;
import com.now.printer.ui.activity.HtmlPrintActivity;
import com.now.printer.ui.activity.PrintHistoryRecordActivity;
import com.now.printer.utils.FileUtils;

import org.xutils.common.util.LogUtil;

import java.io.File;
import java.util.Objects;

import static android.app.Activity.RESULT_OK;

public class MoreContainerFragment extends BaseFragment2 {

    @Override
    protected void onLazy() {

    }

    public int getLayoutId() {
        return R.layout.fragment_container_more;
    }

    @Override
    public void doBusiness(Context mContext, View convertView) {
        convertView.findViewById(R.id.rl_div_file_brower).setOnClickListener(onClickListener);
        convertView.findViewById(R.id.rl_div_idcard_print).setOnClickListener(onClickListener);
        convertView.findViewById(R.id.rl_html_print).setOnClickListener(onClickListener);
        convertView.findViewById(R.id.rv_div_print_record).setOnClickListener(onClickListener);

        nbToolbar.setCenterTitle(getString(R.string.more));
        nbToolbar.setShowSearch();
        nbToolbar.setToolbarLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BaseActivity2) getActivity()).openMenu();
            }
        });
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.rl_div_file_brower:
                    Intent intent = new Intent();
                    intent.putExtra("type", "1");//0是所有，1是文件浏览，2是图片
                    intent.setClass(getActivity(), FileBrowerManagerActivity.class);
                    getActivity().startActivity(intent);
                    break;
                case R.id.rl_div_idcard_print:

                    Intent intent5 = new Intent(getActivity(), CameraActivity.class);
                    idCardFrontImgPath = FileUtils.getCurrentAppPath(Objects.requireNonNull(getActivity())) + File.separator  + "正面照"  + ".png";
                    frontAndBackImagePath[0] = idCardFrontImgPath;

                    intent5.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,idCardFrontImgPath);
                    intent5.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
                    startActivityForResult(intent5, REQUEST_CODE);
                    break;
                case R.id.rl_html_print:
                    Intent intent2 = new Intent();
                    intent2.setClass(getActivity(), HtmlPrintActivity.class);
                    getActivity().startActivity(intent2);
                    break;
                case R.id.rv_div_print_record:
                    Intent intent3 = new Intent(getActivity(), PrintHistoryRecordActivity.class);
                    startActivity(intent3);
                    break;
            }

        }
    };


    private static final int REQUEST_CODE = 1001;
    private String[] frontAndBackImagePath = new String[2];
    private String idCardFrontImgPath = "";
    private String idCardBackImgPath = "";

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null){
            String idCardFront = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);

            if (TextUtils.equals(idCardFront,CameraActivity.CONTENT_TYPE_ID_CARD_FRONT)){
//                frontAndBackImagePath[0] = data.getStringExtra(CameraActivity.KEY_OUTPUT_FILE_PATH);

                Intent intent = new Intent(getActivity(), CameraActivity.class);
                idCardBackImgPath = FileUtils.getCurrentAppPath(Objects.requireNonNull(getActivity())) + File.separator + "反面照" + ".png";
                frontAndBackImagePath[1] = idCardBackImgPath;

                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,idCardBackImgPath);
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
                startActivityForResult(intent, REQUEST_CODE);

            }else if (TextUtils.equals(idCardFront,CameraActivity.CONTENT_TYPE_ID_CARD_BACK)){
//
//                frontAndBackImagePath[1] = data.getStringExtra(CameraActivity.KEY_OUTPUT_FILE_PATH);
                LogUtil.i("current type id card front 正面照地址： " + frontAndBackImagePath[0]);
                LogUtil.i("current type id card back 反面照地址：" + frontAndBackImagePath[1]);

                Intent intent = new Intent(getActivity(), FilePreviewAndPrintActivity.class);
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_ID_CARD_FRONT_BACK, frontAndBackImagePath);
//            ArrayList<String> pathList = data.getStringArrayListExtra(CameraActivity.INTENT_KEY_ID_CARD_FRONT_BACK);
                startActivity(intent);
            }
        }
    }


}
